//
//  ShopCell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/10/30.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "ShopCell.h"
#import "ShopModel.h"
#import "ShopSubModel.h"

@interface ShopCell()
@property (weak, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UIImageView *shopIV;
@property (weak, nonatomic) IBOutlet UILabel *titleNamedL;
@property (weak, nonatomic) IBOutlet UILabel *titleSubL;
@property (weak, nonatomic) IBOutlet UILabel *serviceScoreL;
@property (weak, nonatomic) IBOutlet UILabel *fansL;
@property (weak, nonatomic) IBOutlet UIButton *collectionBtn;
@property (weak, nonatomic) IBOutlet UIButton *enterShopBtn;
@property (weak, nonatomic) IBOutlet UIImageView *startOneIV;
@property (weak, nonatomic) IBOutlet UIImageView *startTwoIV;
@property (weak, nonatomic) IBOutlet UIImageView *startThreeIV;
@property (weak, nonatomic) IBOutlet UIImageView *startFourIV;
@property (weak, nonatomic) IBOutlet UIImageView *startFiveIV;
@property (weak, nonatomic) IBOutlet UILabel *pointL;

@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,assign) int b;
@end
@implementation ShopCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.manager = [AFHTTPSessionManager manager];
    self.view.layer.borderColor  = [UIColor colorWithRed:201/255.0 green:201/255.0 blue:201/255.0 alpha:1].CGColor;
    self.view.layer.borderWidth  = 3;
    [self.collectionBtn setTitleColor:kLabel102Color forState:UIControlStateNormal];
    [self.collectionBtn setTitle:@"添加收藏" forState:UIControlStateNormal];
    [self.collectionBtn setTitle:@"已收藏" forState:UIControlStateSelected];
    self.collectionBtn.layer.masksToBounds = YES;
    self.collectionBtn.layer.cornerRadius = 3;
    self.collectionBtn.layer.borderColor  = kLineColor.CGColor;
    self.collectionBtn.layer.borderWidth  = 1;
    [self.enterShopBtn setTitleColor:kOrangeBg forState:UIControlStateNormal];
    [self.enterShopBtn setTitleColor:kOrangeBg forState:UIControlStateSelected];
    self.enterShopBtn.layer.masksToBounds = YES;
    self.enterShopBtn.layer.cornerRadius = 3;
    self.enterShopBtn.layer.borderColor  = kOrangeBg.CGColor;
    self.enterShopBtn.layer.borderWidth  = 1;
    self.enterShopBtn.selected = YES;

}
- (IBAction)collectionBtn:(UIButton *)sender {
    UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    if (self.shopSubModell.uid == [[UserDefaultsHelper readUid]integerValue]) {
        [WSProgressHUD showImage:[UIImage imageNamed:@""] status:@"不可以收藏自己的店铺"];
    }else{
        if ([[UserDefaultsHelper readAutoLogIn] isEqualToString:@"NO"]||[UserDefaultsHelper readAutoLogIn].length == 0) {
            LogInOrOutVC *logVC = [[LogInOrOutVC alloc]init];
            MainNavC *mianNav = [[MainNavC alloc]initWithRootViewController:logVC];
            [viewController presentViewController:mianNav animated:YES completion:nil];
        }else{
            
            self.dict = @{
                          @"userId":[UserDefaultsHelper readUid],
                          @"token":[UserDefaultsHelper readToken]
                          };
            self.urlString = [NSString stringWithFormat:@"%@/user/checkToken",kUrl];
            [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                XLog(@"responseObject:%@",responseObject);
                NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
                if (status == 200) {
                    sender.selected = !sender.selected;
                    if ([self.delegate respondsToSelector:@selector(shopCell:withBtn:withModel:)]) {
                        [self.delegate shopCell:self withBtn:sender withModel:self.shopModel];
                    }
                    if ([self.delegate respondsToSelector:@selector(shopCell:withBtn:withSubModel:)]) {
                        [self.delegate shopCell:self withBtn:sender withSubModel:self.shopSubModell];
                    }
                }else {
                    [UIAlertViewWarningAccount showWarningAccount];
                    [LogOutAccount presentOutAccount];
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        }
    }
}
- (IBAction)enterBtn:(UIButton *)sender {
//    if ([self.delegate respondsToSelector:@selector(shopCell:withBtn:withModel:)]) {
//        [self.delegate shopCell:self withBtn:sender withModel:self.shopModel];
//    }
    if ([self.delegate respondsToSelector:@selector(shopCell:withBtn:withSubModel:)]) {
        [self.delegate shopCell:self withBtn:sender withSubModel:self.shopSubModell];
    }
}
- (void)setModel:(ShopModel *)model
{
    _model = model;
    self.titleNamedL.text = model.title;
    self.titleSubL.text = model.represent;
    //    self.serviceScoreL.text = [NSString stringWithFormat:@"服务项目:%ld个",shopModel.assCount];
    self.fansL.text = [NSString stringWithFormat:@"服务项目:   %ld个",model.assCount];
    
    int a = 0;
    double c = 0;
    self.b = 0;
    //向下
    a = floor(model.evaluate)/10;
    XLog(@"a = %d",a);
    c = model.evaluate/10-a;
    c=c*10;
    XLog(@"c = %.0f",c);
    if (c == 0) {
        self.b = model.evaluate/10;
        [self startCount];
    }else if(c>=5){
        //    向上
        self.b = ceil(model.evaluate/10);
        [self startCount];
    }else {
        self.b= a;
        [self startHalfCount];//半颗星
    }
    self.pointL.text = [NSString stringWithFormat:@"%.1f分",model.evaluate/10*5];
}
- (void)setShopModel:(ShopModel *)shopModel
{
    _shopModel = shopModel;
//    ShopSubModel *subModel = shopModel.data[0];
    XLog(@"subModel.title:%@",self.shopModel.title);
    self.titleNamedL.text = shopModel.title;
    self.titleSubL.text = shopModel.represent;
//    self.serviceScoreL.text = [NSString stringWithFormat:@"服务项目:%ld个",shopModel.assCount];
    self.fansL.text = [NSString stringWithFormat:@"服务项目:   %ld个",shopModel.assCount];
    
    int a = 0;
    double c = 0;
    self.b = 0;
    //向下
    a = floor(shopModel.evaluate)/10*5;
    XLog(@"a = %d",a);
    c = shopModel.evaluate/10*5-a;
    c=c*10;
    XLog(@"c = %.0f",c);
    if (c == 0) {
        self.b = shopModel.evaluate/10*5;
        [self startCount];
    }else if(c>=5){
        //    向上
        self.b = ceil(shopModel.evaluate/10*5);
        [self startCount];
    }else {
        self.b= a;
        [self startHalfCount];//半颗星
    }
    self.pointL.text = [NSString stringWithFormat:@"%.1f分",shopModel.evaluate/10*5];
    
}
- (void)setShopSubModell:(ShopSubModel *)shopSubModell
{
    _shopSubModell = shopSubModell;
    [self.shopIV sd_setImageWithURL:[NSURL URLWithString:shopSubModell.logo] placeholderImage:[UIImage imageNamed:@"headImg"]];
    
     if (shopSubModell.type == 2) {
        self.titleSubL.text = @"企业提供";
        self.titleNamedL.text = [NSString stringWithFormat:@"%@官方旗舰店",shopSubModell.title];
     }else {
         self.titleSubL.text = @"个人提供";
         self.titleNamedL.text = [NSString stringWithFormat:@"%@的店铺",shopSubModell.title.length?shopSubModell.title:@""];
     }
    //    self.serviceScoreL.text = [NSString stringWithFormat:@"服务项目:%ld个",shopModel.assCount];
    self.fansL.text = [NSString stringWithFormat:@"服务项目: %ld个",shopSubModell.associationCount+[shopSubModell.demandCount integerValue]];
    if (shopSubModell.collectStatus == 0) {
        [self.collectionBtn setTitle:@"添加收藏" forState:UIControlStateNormal];
        self.collectionBtn.selected = NO;
    }else {
        [self.collectionBtn setTitle:@"已添加" forState:UIControlStateNormal];
        self.collectionBtn.selected = YES;
    }
    int a = 0;
    double c = 0;
    self.b = 0;
    //向下
    XLog(@"shopSubModell.evaluate:%f",shopSubModell.evaluate);
    a = floor(shopSubModell.evaluate)/10*5;
    XLog(@"a = %d",a);
    c = shopSubModell.evaluate/10*5-a;
    c=c*10;
    XLog(@"c = %.0f",c);
    if (c == 0) {
        self.b = shopSubModell.evaluate/10*5;
        [self startCount];
    }else if(c>=5){
        //    向上
        self.b = ceil(shopSubModell.evaluate/10*5);
        [self startCount];
    }else {
        self.b= a;
        [self startHalfCount];//半颗星
    }
    self.pointL.text = [NSString stringWithFormat:@"%.1f分",shopSubModell.evaluate/10*5];
}
-(void)startCount
{
    if (self.b== 0) {
        self.startOneIV.image = [UIImage imageNamed:@"icon_07"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_07"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b>=1) {
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_07"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b >=2){
        self.startTwoIV.image = [UIImage imageNamed:@"icon_03"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b >=3){
        self.startThreeIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b >=4){
        self.startFourIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if(self.b >=5){
        self.startFiveIV.image = [UIImage imageNamed:@"icon_03"];
    }
    
}
-(void)startHalfCount
{
    
    if (self.b==1) {
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_05"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b == 2){
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_03"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_05"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_07"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b ==3){
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_03"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_05"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_07"];
    }
    if (self.b ==4){
        self.startOneIV.image = [UIImage imageNamed:@"icon_03"];
        self.startTwoIV.image = [UIImage imageNamed:@"icon_03"];
        self.startThreeIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFourIV.image = [UIImage imageNamed:@"icon_03"];
        self.startFiveIV.image = [UIImage imageNamed:@"icon_05"];
    }
    
}
@end
